home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Borland / Borland C++ V5.02 / STDLIB.PAK / COMPLX.CPP < prev    next >
C/C++ Source or Header  |  1997-05-06  |  924b  |  40 lines

  1. /**************************************************************************
  2.  *
  3.  * complx.cpp - Complex Number example program.    Section 14.3
  4.  *
  5.  * $Id: complx.cpp,v 1.3 1995/08/29 19:00:58 oberg Exp $
  6.  *
  7.  * $$RW_INSERT_HEADER "slyrs.cpp"
  8.  *
  9.  **************************************************************************/
  10.  
  11. # include <complex>
  12. # include <utility>
  13.  
  14. # include <iostream.h>
  15. using namespace std;
  16.  
  17. typedef complex<double> dcomplex;
  18.  
  19. pair<dcomplex, dcomplex> quadratic
  20.     (dcomplex a, dcomplex b, dcomplex c)
  21.         // return roots of a quadratic equation
  22. {
  23.     dcomplex root = sqrt(b * b - 4.0 * a * c);
  24.     a = a * 2.0;
  25.     
  26.     return make_pair (
  27.         (-b + root) / a,
  28.         (-b - root) / a);
  29. }
  30.  
  31. int main() {
  32.     dcomplex a(2, 3);
  33.     dcomplex b(4, 5);
  34.     dcomplex c(6, 7);
  35.     
  36.     pair<dcomplex, dcomplex> ans = quadratic(a, b, c);
  37.     cout << "Roots are " << ans.first << " and " << ans.second  << endl;
  38.     return 0;
  39. }
  40.